#include "mc_type.h"
#include "global_variable.h"
#include "app_sub.h"
#include "FW_interface.h"


#if(OBMODE == SMO)
void AdaptiveRamp_Init(STR_Motor_Def *tS_M, STR_SMOVariable *tS_SMO, STR_PIC *tS_PI);
void RampStable(STR_Motor_Def *tS_M, STR_SMOVariable *tS_SMO, STR_PIC *tS_PI);
void ALM_Motor_Lock_Protect(STR_Motor_Def *tS_M, STR_SMOVariable *tS_SMO);
void Motor_DIRDetBEMF(STR_Motor_Def *tS_M, STR_DETBEMF_TypeDef *tS_Det, STR_SMOVariable *tS_SMO, STR_PIC *tS_PLL_PI \
                      , STR_PIC *tS_Id_PI, STR_PIC *tS_Iq_PI, STR_PIC *tS_Spd_PI);
void ThetaEst(STR_SMOVariable *tS_SMO, STR_Motor_Def *tS_M, STR_TrigComponents tS_TrigSinCos);


#define LIB_OB_MOTOR_SPD                gS_SMO.nWr
#define LIB_OB_MOTOR_THETA              gS_SMO.unPLLTheta
#define LIB_OB_RESET_EN()               gS_SMO.ubVarReset = ENABLE
#define LIB_RAMP_STABLE()               RampStable(&gS_Motor, &gS_SMO, &gS_PLL_PI)
#define LIB_ADAPTIVERAMP_INIT()         AdaptiveRamp_Init(&gS_Motor, &gS_SMO, &gS_PLL_PI)
#define LIB_ALM_MOTOR_LOCK_PROTECT()    ALM_Motor_Lock_Protect(&gS_Motor, &gS_SMO)
#define LIB_DIR_DET()                   Motor_DIRDetBEMF(&gS_Motor,&gS_DetBEMF, &gS_SMO, &gS_PLL_PI \
                                            ,&gS_IdPI, &gS_IqPI ,&gS_SPD_PI)
#define LIB_THETA_EST()                 ThetaEst(&gS_SMO, &gS_Motor, gS_Motor.mS_Ctr.mS_TrigSinCos)

#elif (OBMODE == BEMFOB)
void RampStable(STR_Motor_Def *tS_M, STR_BEMFOBVariable *tS_BEMFOB, STR_PIC *tS_PI);
void AdaptiveRamp_Init(STR_Motor_Def *tS_M, STR_BEMFOBVariable *tS_BEMFOB, STR_PIC *tS_PI);
void ALM_Motor_Lock_Protect(STR_Motor_Def *tS_M, STR_BEMFOBVariable *tS_BEMFOB);
//void Motor_DIRDetBEMF(STR_Motor_Def *tS_M, STR_DETBEMF_TypeDef *tS_Det, STR_BEMFOBVariable *tS_BEMFOB, STR_PIC *tS_PLL_PI \
                      , STR_PIC *tS_Id_PI, STR_PIC *tS_Iq_PI, STR_PIC *tS_Spd_PI);
void ThetaEST_BEMFOB(STR_Motor_Def *tS_M, STR_BEMFOBVariable *tS_BEMFOB, STR_PIC *tS_PI);

#define LIB_OB_MOTOR_SPD                gS_BEMFOB.nWr
#define LIB_OB_MOTOR_THETA              gS_BEMFOB.unThetaOut
#define LIB_OB_RESET_EN()               gS_BEMFOB.ubVarReset = ENABLE;
#define LIB_RAMP_STABLE()               RampStable(&gS_Motor, &gS_BEMFOB, &gS_BEMFOB_PLL_PI)
#define LIB_ADAPTIVERAMP_INIT()         AdaptiveRamp_Init(&gS_Motor, &gS_BEMFOB, &gS_BEMFOB_PLL_PI)
#define LIB_ALM_MOTOR_LOCK_PROTECT()    ALM_Motor_Lock_Protect(&gS_Motor, &gS_BEMFOB)
#define LIB_DIR_DET()                   Motor_DIRDetBEMF(&gS_Motor,&gS_DetBEMF, &gS_BEMFOB, &gS_BEMFOB_PLL_PI \
                                            ,&gS_IdPI, &gS_IqPI ,&gS_SPD_PI)
#define LIB_THETA_EST()                 ThetaEST_BEMFOB(&gS_Motor, &gS_BEMFOB, &gS_BEMFOB_PLL_PI)

#endif





void SVPWM_2R(STR_Motor_Def *tS_M, STR_SVM_TypeDef *tS_SVM);
void gS_VsLimitation(STR_Motor_Def *tS_M);
void GetVbusComp(STR_Motor_Def *tS_M);
void VbusCompFunction(STR_Motor_Def *tS_M);
void IPD(STR_IPDVariable *tS_IPD, STR_Motor_Def *tS_M);
u8 ubSpd_Det(STR_Motor_Def *tS_M);
void ALM_LostPhase(STR_Motor_Def *tS_M);
void ReverseLIB(STR_Motor_Def *tS_M);
void ReverseRampInitLIB(STR_Motor_Def *tS_M);


